# coding: utf-8
import time
import psutil
import redis

r = redis.Redis(host='127.0.0.1', port=6379, db=0)
print r.get('a3')
print r.dbsize()
r.hmset("net", {"www": "vdin", "project": "vdin"})
r.lpush("net1", [1, 2, 3, 4, 5])
if r.get("total_recv") is None:
    r.set("total_recv", 0)
if r.get("total_sent") is None:
    r.set("total_sent", 0)
    # todo: 总流量可以从mysql数据库中读取
print r.get("total_sent"), type(r.get("total_sent"))
# while True:
#     net = psutil.net_io_counters()
#     recv_bytes = long(net.bytes_recv)
#     sent_bytes = long(net.bytes_sent)
#     t = int(time.time())
#     r.set("total_recv", recv_bytes)
#     r.set("total_sent", sent_bytes)
#     r.hmset("net_recv" + time.strftime("%Y%m%d"), {t: recv_bytes})
#     r.expire("net_recv" + time.strftime("%Y%m%d"), 24 * 60 * 60)
#     r.hmset("net_sent" + time.strftime("%Y%m%d"), {t: sent_bytes})
#     r.expire("net_sent" + time.strftime("%Y%m%d"), 24 * 60 * 60)
#     time.sleep(1)
recv_1 = long(psutil.net_io_counters().bytes_recv)
sent_1 = long(psutil.net_io_counters().bytes_sent)
# while True:
#     time.sleep(60)
#     recv_2 = long(psutil.net_io_counters().bytes_recv)
#     sent_2 = long(psutil.net_io_counters().bytes_sent)
#     t = time.strftime("%H%M")
#     r.hmset("recv_5min" + time.strftime("%Y%m%d"), {t: recv_2-recv_1})
#     r.expire("recv_5min" + time.strftime("%Y%m%d"), 24 * 60 * 60)
#     r.hmset("sent_5min" + time.strftime("%Y%m%d"), {t: sent_2 - sent_1})
#     r.expire("sent_5min" + time.strftime("%Y%m%d"), 24 * 60 * 60)
#     recv_1, sent_1 = recv_2, sent_2

print r.hmget('fw_tx_20171129',"1511925239", "15119252409", "1511925240")
print r.hexists('fw_tx_20171129', 1511925240)

r.rpush('flow_tx', 123, 456)
print r.llen('flow_tx')